import * as moment from 'moment';
import { TagEntity } from 'src/module/tag/entities/tag.entity';
import { UsersEntity } from 'src/module/user/entities/user.entity';
import {
  BeforeInsert,
  BeforeUpdate,
  Column,
  Entity,
  JoinColumn,
  ManyToMany,
  ManyToOne,
  PrimaryColumn,
} from 'typeorm';
import { v4 as uuidv4 } from 'uuid';

@Entity({ name: 'essay' })
export class EssayEntity {
  @PrimaryColumn()
  id: string = uuidv4();

  @Column({ type: 'varchar', length: 60 })
  essaytitle: string;

  @Column({ type: 'text' })
  content: string;

  @Column({ type: 'tinyint', default: 0 })
  watermarking: number;

  @Column({ type: 'tinyint', default: 0 })
  del: number;

  @ManyToMany(() => TagEntity, (tag) => tag.essays, {
    cascade: true,
    onDelete: 'CASCADE',
  })
  tags: TagEntity[];

  @ManyToOne(() => UsersEntity, (user) => user.essays)
  @JoinColumn({ name: 'user_id' })
  user: UsersEntity;

  @Column({ type: 'int', default: 0 })
  view_count: number;

  
  @Column({ type: 'int', default: 0 })
  status: number;

  @Column({ type: 'varchar', nullable: true })
  created_at: string;

  @Column({ type: 'varchar', nullable: true })
  updated_at: string;

  @BeforeInsert()
  createDates() {
    this.created_at = moment().format('YYYY-MM-DD HH:mm:ss');
  }

  @BeforeUpdate()
  updateDates() {
    this.updated_at = moment().format('YYYY-MM-DD HH:mm:ss');
  }
}
